import { ElMessage } from "element-plus"
import localCache from "../../../utils/cache"
import type ITaskData from "../types/ITaskData"
import { UNDERWAYTASKID, COMPLETEDTASKID } from "../constant/taskId"
import { taskName } from "../enum/taskName"

/**
 * 删除任务
 * @param taskData 任务数据数组
 * @param taskType 任务类型
 */
export const remove = (taskData: ITaskData, taskType: string): void => {

  switch (taskType){
    case taskName.UNDERWAY:
      if(taskData.underwayTaskData.length == 0){
        ElMessage({
          showClose: true,
          message: '没有数据可清空',
          type: 'info',
        })
        return
      }

      taskData.underwayTaskData = []
      break

    case taskName.COMPLETED:
      if(taskData.completedTaskData.length == 0){
        ElMessage({
          showClose: true,
          message: '没有数据可清空',
          type: 'info',
        })
        return
      }

      taskData.completedTaskData = []
      break
  }

  localCache.clearCache()
  taskData.underwayTaskData.forEach(item => {
    localCache.setCache(UNDERWAYTASKID + item.id, item.title)
  })
  taskData.completedTaskData.forEach(item => {
    localCache.setCache(COMPLETEDTASKID + item.id, item.title)
  })
  ElMessage({
      showClose: true,
      message: '清空成功',
      type: 'success',
    })
}

/**
 * 删除所有任务
 * @param taskData 任务数据数组
 */
export const removeAll = (taskData: ITaskData): void => {
  if(taskData.completedTaskData.length + taskData.underwayTaskData.length == 0){
    ElMessage({
      showClose: true,
      message: '没有数据可清空',
      type: 'info',
    })
    return
  }
  taskData.completedTaskData = []
  taskData.underwayTaskData = []
  localCache.clearCache()

  ElMessage({
      showClose: true,
      message: '清空成功',
      type: 'success',
    })
}

